<template>
  <div style="width: 100%; height: 100%">
    <h1>简历筛选</h1>
    <!-- 查询框 -->
    <div>
      <el-input v-model="keyword" style="width: 300px; margin-left: 10px; margin-right: 10px" placeholder="请输入关键词"></el-input>
      <el-button type="primary" @click="fetchAnnouncements">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </div>

    <el-tabs v-model="activeTab">
      <el-tab-pane label="查看简历" name="viewResumes">
        <el-table :data="applications" style="width: 100%">
          <el-table-column prop="name" label="申请人" />
          <el-table-column prop="job_title" label="职位名称" />
          <el-table-column prop="applied_at"  label="申请时间" />
          <el-table-column prop="application_status" label="当前状态" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="viewResume(scope.row)">查看简历</el-button>
              <el-button type="success" @click="updateStatus(scope.row, 'shortlisted')">安排面试</el-button>
              <el-button type="danger" @click="updateStatus(scope.row, 'rejected')">拒绝</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="简历筛选管理" name="manageScreening">
        <el-table :data="screeningStatuses" style="width: 100%">
          <el-table-column prop="status" label="状态" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="editStatus(scope.row)">编辑</el-button>
              <el-button type="danger" @click="deleteStatus(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="addStatus">添加状态</el-button>
      </el-tab-pane>
    </el-tabs>

    <el-dialog title="查看简历" :visible.sync="resumeDialogVisible">
      <el-descriptions title="申请人信息">
        <el-descriptions-item label="姓名">{{ currentApplicant.name }}</el-descriptions-item>
        <el-descriptions-item label="出生日期">{{ currentApplicant.dob }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ currentApplicant.gender }}</el-descriptions-item>
        <el-descriptions-item label="联系方式">{{ currentApplicant.phone }}</el-descriptions-item>
        <el-descriptions-item label="地址">{{ currentApplicant.address }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions title="简历信息">
        <el-descriptions-item label="教育背景">{{ currentApplicant.education }}</el-descriptions-item>
        <el-descriptions-item label="工作经验">{{ currentApplicant.experience }}</el-descriptions-item>
        <el-descriptions-item label="技能">{{ currentApplicant.skills }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      activeTab: 'viewResumes',
      applications: [],
      screeningStatuses: [],
      currentApplicant: {},
      resumeDialogVisible: false
    }
  },
  created() {
    this.fetchApplications()
    this.fetchScreeningStatuses()
  },
  methods: {
    fetchApplications() {
      axios.get('http://localhost:8080/applications/getAllApplications')
          .then(response => {
            this.applications = response.data.data.filter(item => item.company_id == localStorage.getItem('company_id')&& item.application_status==='submitted');
            console.log(this.applications)
            })
          .catch(error => {
            console.error('获取申请记录失败:', error)
          })
    },
    viewResume(row) {
      this.currentApplicant=row
      this.resumeDialogVisible=true;
    },
    updateStatus(row, newStatus) {
      row.application_status=newStatus
      console.log(newStatus)
      axios.put(`http://localhost:8080/applications/new`, row)
          .then(response => {
            this.$message.success('状态更新成功')
            this.fetchApplications()
          })
          .catch(error => {
            console.error('更新申请状态失败:', error)
          })
    },
    editStatus(status) {
      // 编辑筛选状态的逻辑
    },
    deleteStatus(status) {
      axios.delete(`/api/screening-statuses/${status.id}`)
          .then(response => {
            this.$message.success('删除状态成功')
            this.fetchScreeningStatuses()
          })
          .catch(error => {
            console.error('删除状态失败:', error)
          })
    },
    addStatus() {
      // 添加新筛选状态的逻辑
    }
  }
}
</script>